/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package in.jquickfinder.extensionviewer;

import java.awt.BorderLayout;
import java.util.Properties;
import java.util.logging.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//in.jquickfinder.extensionviewer//FileExtensionViewer//EN",
autostore = false)
@TopComponent.Description(preferredID = "FileExtensionViewerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "in.jquickfinder.extensionviewer.FileExtensionViewerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_FileExtensionViewerAction",
preferredID = "FileExtensionViewerTopComponent")
public final class FileExtensionViewerTopComponent extends TopComponent implements ExplorerManager.Provider {

    private static FileExtensionViewerTopComponent instance;
    private ExplorerManager em = new ExplorerManager();
    private static final String PREFERRED_ID = "FileExtensionViewerTopComponent";

    public FileExtensionViewerTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(FileExtensionViewerTopComponent.class, "CTL_FileExtensionViewerTopComponent"));
        setToolTipText(NbBundle.getMessage(FileExtensionViewerTopComponent.class, "HINT_FileExtensionViewerTopComponent"));
        FileExtensionChildFactory factory = new FileExtensionChildFactory();
        Children extensionNodes = Children.create(factory, true);
        AbstractNode rootNode = new AbstractNode(extensionNodes);
        setLayout(new BorderLayout());
        OutlineView outlineView = new OutlineView();
        rootNode.setDisplayName("File Extensions");
        outlineView.setDragSource(true);
        em.setRootContext(rootNode);
        add(outlineView, BorderLayout.CENTER);
        associateLookup(ExplorerUtils.createLookup(em, getActionMap()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    public static synchronized FileExtensionViewerTopComponent getDefault() {
        if (instance == null) {
            instance = new FileExtensionViewerTopComponent();
        }
        return instance;
    }

    public static synchronized FileExtensionViewerTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(FileExtensionViewerTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        Logger.getLogger(FileExtensionViewerTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return em;
    }

    private void readPropertiesImpl(Properties p) {
        String version = p.getProperty("version");
    }
}
